/**
 * 
 */
package com.flashdeck.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.flashdeck.R;
import com.flashdeck.StartupActivity;
import com.flashdeck.domain.Feedback;
import com.flashdeck.domain.Feedback.Sender;

/**
 * @author Vincentas
 * 
 */
public class FeedbackActivity extends Activity {

	private Button sendButton;
	
	private EditText subjectEditText;
	
	private EditText messageEditText;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.feedback_layout);
		
		sendButton = (Button) findViewById(R.id.sendButton);
		subjectEditText = (EditText) findViewById(R.id.subjectEditText);
		messageEditText = (EditText) findViewById(R.id.messageEditText);
		
		sendButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Feedback feedback = new Feedback(
					subjectEditText.getText().toString(), 
					messageEditText.getText().toString());
				
				final Feedback.Sender[] senders = new Feedback.Sender[1];
				
				final ProgressDialog dialog = ProgressDialog.show(
					FeedbackActivity.this, 
					getString(R.string.feedback_sending_title), 
					getString(R.string.feedback_sending_message), 
					true, 
					true , 
					new DialogInterface.OnCancelListener() {
						
						@Override
						public void onCancel(DialogInterface dialog) {
							Feedback.Sender sender = senders[0];
							if (sender != null) {
								sender.cancel();
							}
						}
				
					});
				
				senders[0] = feedback.send(new Feedback.ProgressListener() {
					
					@Override
					public void onSuccess() {
						dialog.dismiss();
						FeedbackActivity.this.finish();
					}
					
					@Override
					public void onFailure() {
												
					}
				});
			}
			
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		new AlertDialog.Builder(this).
			setIcon(android.R.drawable.ic_dialog_alert).
			setTitle(R.string.feedback_quit_title).
			setMessage(R.string.feedback_quit_message).
			setPositiveButton(android.R.string.yes, 
					new DialogInterface.OnClickListener() {
		
						@Override
						public void onClick(DialogInterface dialog,	int which) {
							FeedbackActivity.this.finish();
						}
					}).
			setNegativeButton(android.R.string.no, null).
			show();
	}

}
